<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\route\Dispatch;

/**
 * 核销人员
 */
class Remoter extends Backend
{
    /**
     * Remoter模型对象
     * @var object
     * @phpstan-var \app\common\model\Remoter
     */
    protected object $model;

    protected array|string $preExcludeFields = ['id'];

    protected string|array $quickSearchField = ['id'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new \app\common\model\Remoter();
    }

    public function export(): void
    {
        if ($this->request->isPost()) {
            $path = $this->request->post('path');
            $exclePath = public_path().str_replace('/',DIRECTORY_SEPARATOR,$path);
            $spreadsheet = IOFactory::load($exclePath);
            $worksheet = $spreadsheet->getSheet(0);
            $rows = $worksheet->getHighestDataRow();//总行数
            $mobile = [];
            for ($i = 2; $i <= $rows; $i++) {
                $mobile[] =[
                    'mobile' => $worksheet->getCell([1, $i])->getValue(),
                ];
            }
            $this->model->replace()->insertAll($mobile);
            $this->success('导入成功', $mobile);
        }
    }
    /**
     * 若需重写查看、编辑、删除等方法，请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
     */
}